﻿using System;
using System.ComponentModel;
using FluentParser.Expressions;

namespace FluentParser.Editor
{
    public class NodeDecorator
    {
        public NodeDecorator(NodeDecorator parent, ParsingNodeResult source)
        {
            Parent = parent;
            Source = source;
            Operation = source.Expression.TypeOperation;
            ExpressionName = TypeDescriptor.From(source.Expression.GetType()).Name;
            IsSuccess = source.IsSuccess;
            Value = source.Value;
            Range = String.Concat("[", source.Index, " - ", source.End, "]");
            Nodes = new BindingList<NodeDecorator>();

            foreach (ParsingNodeResult result in source.Nodes)
                Nodes.Add(new NodeDecorator(this, result));
        }

        public NodeDecorator Parent { get; private set; }
        public ParsingNodeResult Source { get; private set; }
        public string ExpressionName { get; private set; }
        public OperationType Operation { get; private set; }
        public bool IsSuccess { get; private set; }
        public object Value { get; private set; }
        public string Range { get; private set; }

        public void Delete()
        {
            int index = Parent.Nodes.IndexOf(this);
            Parent.Nodes.RemoveAt(index);
            foreach (NodeDecorator child in Nodes)
            {
                Parent.Nodes.Insert(index++, child);
                child.Parent = Parent;
            }
        }

        public BindingList<NodeDecorator> Nodes { get; private set; }
    }
}
